package com.ruicar.afs.cloud.afscase.approveprev.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.approveprev.condition.ApprovePrevCondition;
import com.ruicar.afs.cloud.afscase.approveprev.entity.CaseApprovePrevInfo;
import com.ruicar.afs.cloud.afscase.approveprev.service.CaseApprovePrevInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 预审批
 * @Author Peng Jinbo
 * @Date 2020/08/08 15:40
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/approvePrev")
public class ApprovePrevController {

    /**
     * Case approve prev info service
     */
    private CaseApprovePrevInfoService caseApprovePrevInfoService;

    /**
     * Author Peng Jinbo
     * List approve prev response
     *
     * @param   condition
     * @return the response
     */
    @GetMapping("/listApprovePrev")
    public IResponse<IPage<CaseApprovePrevInfo>> listApprovePrev(@ModelAttribute ApprovePrevCondition condition) {

        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(condition.getApplyDateStart()) && StringUtils.isNotBlank(condition.getApplyDateEnd() )) {
            startTime = condition.getApplyDateStart();
            endTime = condition.getApplyDateEnd();
        }
            return IResponse.success(caseApprovePrevInfoService.page(
                    new Page<>(condition.getPageNumber(), condition.getPageSize()),
                    Wrappers.<CaseApprovePrevInfo>query().lambda()
                            .like(StringUtils.isNotBlank(condition.getCustomerName()), CaseApprovePrevInfo::getCustName, condition.getCustomerName())
                            .like(StringUtils.isNotBlank(condition.getDealerName()), CaseApprovePrevInfo::getChannelName, condition.getDealerName())
                            .eq(StringUtils.isNotBlank(condition.getCardNo()), CaseApprovePrevInfo::getCertNo, condition.getCardNo())
                            .eq(StringUtils.isNotBlank(condition.getPhone()), CaseApprovePrevInfo::getTelPhone, condition.getPhone())
                            .eq(StringUtils.isNotBlank(condition.getApplyStatus()), CaseApprovePrevInfo::getApplyStatus, condition.getApplyStatus())
                            .ge(StringUtils.isNotBlank(startTime), CaseApprovePrevInfo::getApplyDate, StringUtils.isNotBlank(startTime)?DateUtil.beginOfDay(DateUtil.parse(startTime)):null)
                            .le(StringUtils.isNotBlank(endTime), CaseApprovePrevInfo::getApplyDate, StringUtils.isNotBlank(endTime)?DateUtil.endOfDay(DateUtil.parse(endTime)):null)
                            .orderByDesc(CaseApprovePrevInfo::getApplyDate)
            ));
        }

    /**
     * 获取拒绝原因
     * @param condition
     * @return
     */
    @GetMapping("/queryRefuseReason")
    public IResponse queryRefuseReason (@ModelAttribute ApprovePrevCondition condition) {
        CaseApprovePrevInfo info = caseApprovePrevInfoService.getOne(Wrappers.<CaseApprovePrevInfo>lambdaQuery()
                .eq(CaseApprovePrevInfo::getApplyNo, condition.getApplyNo()));
        String refuseReason = info.getRefuseReason();
        return IResponse.success(refuseReason);
    }
}
